package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hcz
 * @describtion TODO
 * @date 2019-06-26 16:26
 */
@RestController
@RequestMapping("spec")
public class SpecGropController {

    @Autowired
    private SpecService specService;

    /**
     * 根据商品分类查询规格组
     *
     * @param id 商品分类id
     * @return 规格组集合
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategory(@RequestParam("id") Long id) {
        return ResponseEntity.ok(specService.queryGroupByCategory(id));
    }


    /**
     * 根据规格组id查询规格参数
     *
     * @param gid
     * @return 规格组集合
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(@RequestParam("gid") Long gid) {
        return ResponseEntity.ok(specService.querySpecParams(gid));
    }


    /**
     * 添加规格组
     *
     * @param specGroupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        specService.saveSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("/group/{id}")
    public ResponseEntity<Void> deleteSpecGroup(@PathVariable("id")Long gid) {
        specService.deleteSpecParams(gid);
        return ResponseEntity.status(HttpStatus.ACCEPTED).build();
    }



    @PostMapping("/param")
    public ResponseEntity<Void> saveSpecParams(@RequestBody SpecParamDTO specParamDTO){
         specService.saveSpecParams(specParamDTO);
        return ResponseEntity.status(HttpStatus.ACCEPTED).build();
    }

}
